package org.dru.clay.util.file;

import org.dru.clay.util.functional.Iterators;
import org.dru.clay.util.functional.Predicate;

import java.io.File;
import java.util.Iterator;

/**
 * FilteredFileSet
 * User: joakimd
 * Date: 2013-07-24
 * Time: 14:04
 */
public class FilteredFileSet implements FileSet {
    private final FileSet target;
    private final Predicate<File> predicate;

    public FilteredFileSet(final FileSet target, final Predicate<File> predicate) {
        target.getClass();
        predicate.getClass();
        this.target = target;
        this.predicate = predicate;
    }

    public FileSet getTarget() {
        return target;
    }

    public Predicate<File> getPredicate() {
        return predicate;
    }

    @Override
    public File getDir() {
        return target.getDir();
    }

    @Override
    public Iterator<File> iterator() {
        return Iterators.filter(getTarget().iterator(), getPredicate());
    }

    @Override
    public FileSet filter(final Predicate<File> predicate) {
        return new FilteredFileSet(this, predicate);
    }
}
